<template>
	<view class="page">
		<view style="height: 90%;">
			<view class="item">
				<span class="item-font">头像</span>
				<view class="item-conent">
					<image class="avatar" mode="aspectFit" :src="memberInfo.avatar" @error="imageError"></image>
					<uni-icons type="right" size="12"></uni-icons>
				</view>
			</view>
			<view class="item">
				<span class="item-font">用户名</span>
				<view class="item-conent" @click="usernameInput=true">
					<span v-if="!usernameInput">{{memberInfo.username}}</span>
					<input v-else class="uni-input item-conent" @blur="cancelUsername" v-model="memberInfo.username"
						focus />
					<uni-icons type="right" size="12"></uni-icons>
				</view>
			</view>
			<view class="item">
				<span class="item-font">性别</span>
				<view class="item-conent" @click="toggleSex('bottom')">
					<span v-if="memberInfo.sex" >{{memberInfo.sex}}</span>
          <span v-else >请选择</span>

          <uni-icons type="right" size="12"></uni-icons>

				</view>
			</view>
			<view class="item">
				<span class="item-font">手机号</span>
				<view class="item-conent" @click="phoneInput=true">
					<span v-if="!phoneInput">{{memberInfo.phone}}</span>
					<input v-else class="uni-input item-conent" @blur="cancelPhone()" v-model="memberInfo.phone"
						focus />
					<uni-icons type="right" size="12"></uni-icons>
				</view>
			</view>
		</view>

		<view class="logout-bt">
			<span style="color: white;" @click="login">退出登录</span>
		</view>

		<uni-popup ref="popup" background-color="#fff">
			<view class="popup-content">
				<picker-view :value="value" class="picker-view" @change="bindChange">
					<picker-view-column>
						<view class="picker-item" key="0">男</view>
						<view class="picker-item" key="1">女</view>
					</picker-view-column>
				</picker-view>
			</view>

			<view class="popup-bottom">
				<view class="cancel-btn" @click="cancelSex">
					<text style="color: #05c160;">取消</text>
				</view>
				<view class="save-btn" @click="saveSex">
					<text style="color: white;">确定</text>
				</view>
			</view>

		</uni-popup>

		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>

	</view>
</template>

<script>
	import member from '@/api/member.js'
	import {
		mapActions
	} from "vuex"
	export default {
		data() {
			return {
				msgType:"",
				messageText:"",
				value: [],
				tmpSexValue: "",
				usernameInput: false,
				phoneInput:false,
				memberInfo: this.$store.state
			}
		},
		methods: {
			cancelUsername() {
				if (this.memberInfo.username === "" || this.memberInfo.username.length > 10) {
					this.messageToggle('error', "用户名不能为空且小于10个字符！")
					return
				}
				//用户名修改
				this.usernameInput = false
				this.updateMember()
			},
			cancelPhone(){
				//手机号修改
				this.phoneInput = false
				this.updateMember()
			},
			messageToggle(type,msg) {
				this.msgType = type
				this.messageText = msg
				this.$refs.message.open()
			},
			cancelSex() {
				this.$refs.popup.close()
			},
			saveSex(e) {
				this.$refs.popup.close()
				this.memberInfo.sex = this.tmpSexValue
        this.$set(this.memberInfo,'sex',this.tmpSexValue)
				this.updateMember()

			},
			updateMember() {
				member.update(this.memberInfo).then(res => {
					if (res.data.code === 200) {
						this.$tip.success('修改成功!')
					}
				})
			},
			toggleSex(type) {
				this.type = type
				// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
				this.$refs.popup.open(type)
				if (this.memberInfo.sex === "男") {
					this.value = [0]
				} else {
					this.value = [1]
				}
			},
			bindChange(e) {
				const val = e.detail.value
				if (val[0] === 0) {
					this.tmpSexValue = "男"
				} else {
					this.tmpSexValue = "女"
				}
			}
		}
	}
</script>

<style>
	.page {
		height: 100%;
		padding: 15rpx;
		overflow: hidden;
	}

	.item {
		padding: 20rpx;
		background-color: white;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.item-font {
		font-size: 12px;
	}

	.item-conent {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-end;
		font-size: 12px;
	}



	.logout-bt {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-bottom: 20rpx;
		background-color: #0128aa;
		border-radius: 45rpx;
		height: 70rpx;
	}

	.avatar {
		width: 35px;
		height: 35px;
		border-radius: 5px;
	}

	.popup-content {
		height: 150px;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.picker-view {
		width: 100%;
		height: 200rpx;
		margin-top: 20rpx;
	}

	.picker-item {
		line-height: 100rpx;
		text-align: center;
	}

	.popup-bottom {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		height: 50px;
	}

	.cancel-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 80px;
		height: 30px;
		border-radius: 5px;
		background: #f2f2f2;
		margin-right: 10px;
	}

	.save-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 5px;
		width: 80px;
		height: 30px;
		background: #05c160;
		margin-left: 10px;
	}
</style>
